package com.mxx.common.netty.http.resolvers;

import com.mxx.common.conv.JacksonConverter;
import com.mxx.common.netty.annotation.Path;
import com.mxx.common.netty.http.NettyMethod;
import com.mxx.common.netty.http.NettyHttpRequest;
import com.mxx.common.netty.http.NettyMethodParameter;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/8/4 11:18
 * @Description: 暂无描述
 */
@Component
public class PathValueResolvers extends AbstractNettyResolvers {


    public PathValueResolvers(JacksonConverter jacksonConverter) {
        super(jacksonConverter);
    }

    @Override
    public boolean match(NettyMethod nettyMethod, NettyMethodParameter methodParameter) {
        if (methodParameter.getAnnotation(Path.class) == null) {
            return false;
        }
        flashParamType(nettyMethod, methodParameter);
        return true;
    }

    @Override
    public Object resolvers(NettyMethod nettyMethod, NettyHttpRequest request, NettyMethodParameter methodParameter) throws IOException {
        Path path = methodParameter.getAnnotation(Path.class);
        String pathValue = request.getStringPathValue(path.value());
        if (methodParameter.getType().equals(String.class)) {
            return pathValue;
        }
        return jacksonConverter.convertValue(pathValue, super.getJavaType(nettyMethod, methodParameter));
    }


}
